package com.doudizu.shoesonline.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.doudizu.shoesonline.common.UserContext;
import com.doudizu.shoesonline.constant.UserConstant;
import com.doudizu.shoesonline.mapper.SizeMapper;
import com.doudizu.shoesonline.model.dto.size.SizeQuery;
import com.doudizu.shoesonline.model.entity.Size;
import com.doudizu.shoesonline.model.entity.User;
import com.doudizu.shoesonline.service.SizeService;
import com.doudizu.shoesonline.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* 
* @description 针对表【size(尺码)】的数据库操作Service实现
* @createDate 2023-07-03 09:55:49
*/
@Service
public class SizeServiceImpl extends ServiceImpl<SizeMapper, Size>
    implements SizeService {
    @Autowired
    private UserService userService;

    @Override
    public Object getSize(SizeQuery sizeQuery) {
        Integer pageNum = sizeQuery.getPageNum();
        Integer pageSize = sizeQuery.getPageSize();
        //TODO 可选分页
        if (pageNum==null || pageSize==null){
            pageNum=1;
            pageSize=5;
        }
        Page<Size> objectPage = new Page<>(pageNum, pageSize);
        QueryWrapper<Size> brandQueryWrapper = new QueryWrapper<>();
        User user = userService.getById(UserContext.getUserId());
        if (user != null && UserConstant.ADMIN_ROLE.equals(user.getRole())) {
            if(sizeQuery != null && sizeQuery.getStatus() != null){
                brandQueryWrapper.eq("status", sizeQuery.getStatus());
            }
        }else{
            brandQueryWrapper.eq("status", 1);
        }
        brandQueryWrapper.eq(sizeQuery.getSize() != null,"size",sizeQuery.getSize());
        Page<Size> page = this.page(objectPage, brandQueryWrapper);
        return page;
    }
}




